import {
  queryAlarmData,
  querySimCard,
  addSimCard,
  updateSimCard,
  deleteSimCard,
  queryObdDeviceList, 
  updateObdDevice,
  addObdDevice,
  deleteObdDevice,
  queryPackageList,
  updatePackageInfo,
  addUpdatePackage
} from '@/services/api';

export default {
  namespace: 'obd',

  state: {
    warningList: [],
    simCardList: [],
    deviceList:[],
    packageList:[],
  },

  effects: {
    *fetchObdWarningList({ payload, callback }, { call, put }) {
      const response = yield call(queryAlarmData, payload);
      if (callback) callback(response);
      yield put({
        type: 'warningObdList',
        payload: response,
      });
    },
    *fetchQuerySimCard({ payload, callback }, { call, put }) {
      const response = yield call(querySimCard, payload);
      if (callback) callback(response);
      yield put({
        type: 'querySimCard',
        payload: response,
      });
    },
    *fetchAddSimCard({ payload, callback }, { call, put }) {
      const response = yield call(addSimCard, payload);
      if (callback) callback(response);
    },
    *fetchUpdateSimCard({ payload, callback }, { call, put }) {
      const response = yield call(updateSimCard, payload);
      if (callback) callback(response);
    },
    *fetchDeleteSimCard({ payload, callback }, { call, put }) {
      const response = yield call(deleteSimCard, payload);
      if (callback) callback(response);
    },
    // 终端设备
    *fetchQueryObdDeviceList({ payload, callback }, { call, put }) {
      const response = yield call(queryObdDeviceList, payload);
      if (callback) callback(response);
      yield put({
        type: 'queryObdDeviceList',
        payload: response,
      });
    },
    *fetchUpdateObdDevice({ payload, callback }, { call, put }) {
      const response = yield call(updateObdDevice, payload);
      if (callback) callback(response);
    },
    *fetchAddObdDevice({ payload, callback }, { call, put }) {
      const response = yield call(addObdDevice, payload);
      if (callback) callback(response);
    },
    *fetchDeleteObdDevice({ payload, callback }, { call, put }) {
      const response = yield call(deleteObdDevice, payload);
      if (callback) callback(response);
    },
    // 终端远程升级
    *fetchQueryPackageList({ payload, callback }, { call, put }){
      const response = yield call(queryPackageList, payload);
      if (callback) callback(response);
      yield put({
        type:"resPackageList",
        payload:response
      })
    },
    *fetchUpdatePackageInfo({ payload, callback }, { call, put }){
      const response = yield call(updatePackageInfo, payload);
      if (callback) callback(response);
    },
    *fetchAddUpdatePackage({ payload, callback }, { call, put }){
      const response = yield call(addUpdatePackage, payload);
      if (callback) callback(response);
    }
    // 终端远程配置
  },

  reducers: {
    warningObdList(state, action) {
      return {
        ...state,
        warningList: action.payload,
      };
    },
    querySimCard(state, action) {
      return {
        ...state,
        simCardList: action.payload,
      };
    },
    queryObdDeviceList(state,action){
      return {
        ...state,
        deviceList: action.payload
      }
    },
    resPackageList(state,action){
      return{
        ...state,
        packageList:action.payload
      }
    }
  },
};
